package test.multi_thread;

import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import log.debug.LogInterface;
import log.debug.LogTool;

/**
 * P:658  21.2.4
 * @author yw
 *
 */

class TaskWithResult implements Callable<String> {
	
	
	private int id;
	

	public TaskWithResult(int id) {
		super();
		this.id = id;
	}


	public String call() throws Exception {
		// TODO Auto-generated method stub
		//增加一个耗时任务
		for (long i=0;i<1000000;i++){
			double reuslt = Math.random() * Math.PI / (Math.E * Math.random());
		}
		
		String info = "  ";
		info += "pidInfo==";
		info += ManagementFactory.getRuntimeMXBean().getName();
		info += "\t thread==";
		info += Thread.currentThread().getName();
		
		return "result of TaskWithResult " + id + "\t " + info;
	}
}


public class CallableDemo {
	
	protected static final LogInterface mLog = new LogTool().getLogType();
	private static final String TAG = "CallableDemo";
	
	
	

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String info = "  ";
		info += "pidInfo==";
		info += ManagementFactory.getRuntimeMXBean().getName();
		info += "\t thread==";
		info += Thread.currentThread().getName();
		
		
		ExecutorService exec = Executors.newCachedThreadPool();
		ArrayList<Future<String>> results = new ArrayList<Future<String>>();
		
		mLog.i(TAG, "main  start\t" + info);
		
		for (int i=0; i<5; i++){
			results.add(exec.submit(new TaskWithResult(i)));
		}
		
		exec.shutdown();
		for(Future<String> fs : results){
			try {
				//get blocks utils completion
				mLog.i(TAG, fs.get());
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			mLog.i(TAG, "main  middle \t" + info);
		}
		mLog.i(TAG, "main  end\t" + info);
		System.out.println("main  end  222\t");
		
		/**
		 * 没看懂这个返回结果。。
		 * 为什么 main  end 是最后才打印出来，
		 * 为什么 callable执行顺序正好是  0 1 2 3 4
		 * 
		 * 
		 * 2017-03-04-10:56:19 220  i>>>>:CallableDemo,  main  start	  pidInfo==2853@morandeMacBook-Pro.local	 thread==main
2017-03-04-10:56:22 222  i>>>>:CallableDemo,  result of TaskWithResult 0	   pidInfo==2853@morandeMacBook-Pro.local	 thread==pool-1-thread-1
2017-03-04-10:56:22 222  i>>>>:CallableDemo,  main  middle 	  pidInfo==2853@morandeMacBook-Pro.local	 thread==main
2017-03-04-10:56:22 230  i>>>>:CallableDemo,  result of TaskWithResult 1	   pidInfo==2853@morandeMacBook-Pro.local	 thread==pool-1-thread-2
2017-03-04-10:56:22 230  i>>>>:CallableDemo,  main  middle 	  pidInfo==2853@morandeMacBook-Pro.local	 thread==main
2017-03-04-10:56:22 230  i>>>>:CallableDemo,  result of TaskWithResult 2	   pidInfo==2853@morandeMacBook-Pro.local	 thread==pool-1-thread-3
2017-03-04-10:56:22 230  i>>>>:CallableDemo,  main  middle 	  pidInfo==2853@morandeMacBook-Pro.local	 thread==main
2017-03-04-10:56:22 231  i>>>>:CallableDemo,  result of TaskWithResult 3	   pidInfo==2853@morandeMacBook-Pro.local	 thread==pool-1-thread-4
2017-03-04-10:56:22 231  i>>>>:CallableDemo,  main  middle 	  pidInfo==2853@morandeMacBook-Pro.local	 thread==main
2017-03-04-10:56:22 231  i>>>>:CallableDemo,  result of TaskWithResult 4	   pidInfo==2853@morandeMacBook-Pro.local	 thread==pool-1-thread-5
2017-03-04-10:56:22 231  i>>>>:CallableDemo,  main  middle 	  pidInfo==2853@morandeMacBook-Pro.local	 thread==main
2017-03-04-10:56:22 231  i>>>>:CallableDemo,  main  end	  pidInfo==2853@morandeMacBook-Pro.local	 thread==main
		 *
		 */
	}
		
}
